<?php

namespace System\library;

/**
 * <b>Class</b> :        Http<br>
 * <b>Fonction</b> :     Permet d'effectuer des requetes http, et d'obtenir des informations sur celle-ci
 * <br>
 * @version              1.0
 * @author               Steuf
 * @version              2.0<br>
 *                       amélioration de la fonction de redirection
 * <br>
 * @author               Dread <dreadlokeur@gmail.com>
 * @license              Licence publique générale GNU http://www.gnu.org/licenses/gpl.html
 * @copyright            Copyright 2011 - MidichloriansPHP and contributors
 * @package              System
 * @subpackage           library
 */
class Http {
    const POST = 'POST';
    const GET = 'GET';
    const COOKIE = 'COOKIE';
    const REQUEST = 'REQUEST';

    private static $requestOrder = array('REQUEST');

    /**
     * Get a Query request by key ($_GET)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getQuery($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_GET, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a Post request by key ($_POST)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getPost($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_POST, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a Cookie request by key ($_COOKIE)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getCookie($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_COOKIE, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a server request by key ($_SERVER)
     * 
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getServer($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_SERVER, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a file request by key ($_FILES)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getFile($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_FILES, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a environnement request by key ($_ENV)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    public static function getEnv($key = null, $default = null, $allowHtmlTags = false) {
        return self::getDataFromArray($_ENV, $key, $default, $allowHtmlTags);
    }

    /**
     * Get a request by key (Check in order defined on this class)
     *
     * @access public
     * @static
     * @param Mixed $key By default is null for get all Query values
     * @param Mixed $default Default value if key don't exist
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     *
     * @return Mixed
     */
    public static function getRequest($key = null, $default = null, $allowHtmlTags = false) {
        foreach (self::$requestOrder as &$adata) {
            switch ($adata) {
                case self::POST:
                    $data = self::getDataFromArray($_POST, $key, $default, $allowHtmlTags);
                    break;
                case self::GET:
                    $data = self::getDataFromArray($_GET, $key, $default, $allowHtmlTags);
                    break;
                case self::REQUEST:
                    $data = self::getDataFromArray($_REQUEST, $key, $default, $allowHtmlTags);
                    break;
                case self::COOKIE:
                    $data = self::getDataFromArray($_COOKIE, $key, $default, $allowHtmlTags);
                    break;
                default:
                    throw new \Exception('Whats happening ?');
                    break;
            }
            if ($data !== $default)
                return $data;
        }
        return $default;
    }

    /**
     * Get a data from a reference array
     *
     * @access private
     * @static
     * @param Array $array
     * @param String $key
     * @param Mixed $default
     * @param Mixed $allowHtmlTags By default false : Remove all HTML tags, can be true : allow all html tags, can be a list of accepted HTML tags (see strip_tags documentation)
     * @return Mixed
     */
    private static function getDataFromArray(&$array, $key = false, $default = null, $allowHtmlTags = false) {
        if ($key === false)
            return $array;
        if (!\key_exists($key, $array))
            return $default;
        if ($allowHtmlTags !== true)
            return self::secure($array[$key], $allowHtmlTags);
        return $array[$key];
    }

    /**
     * Secure a value, remove all, or allow html tags
     *
     * @access private
     * @static
     * @param Mixed $value Value to secure
     * @param Mixed $allowHtmlTags It can be a string with HTML tags to allow (see strip_tags documentation)
     * @see http://www.php.net/strip_tags
     * @return Mixed
     */
    private static function secure($value, $allowHtmlTags) {
        if (is_array($value)) {
            foreach ($value as &$v)
                $v = self::secure($v, $allowHtmlTags);
        } elseif (is_string($value)) {
            $value = strip_tags($value, ((is_string($allowHtmlTags) && !empty($allowHtmlTags)) ? $allowHtmlTags : null));
        }
        return $value;
    }

    /**
     * Set order of check parameter for getRequest method (Just add in order on parameter constant of Http class)
     *
     * @access public
     * @static
     * @param void
     * @return void
     *
     * @exemple http->setRequestOrder(Http::POST,Http::GET);
     */
    public static function setRequestOrder() {
        if (func_num_args() == 0)
            throw new \Exception('You must be set at least on parameter');

        $args = func_get_args();
        foreach ($args as &$value)
            if ($value != self::COOKIE && $value != self::GET && $value != self::POST && $value != self::REQUEST)
                throw new \Exception('Invalid parameter "' . $value . "'");
        self::$requestOrder = $args;
    }

    /**
     * Permet de faire une redirection, en javascript si l'header à déja été send, sinon via la fonction header()
     *
     * @access public
     * @static
     * @param string $url l'url de redirection
     * @param mixed $timer temps de redirection: en secondes (option: par defaut 0)
     * @param bool $stop permet de définir si le script doit s'arreter après la redirection, ou non (option: par defaut true)
     * @return void
     */
    public static function redirect($url, $timer = 0, $die = true) {
        if (headers_sent ()) {
            echo '<script language="javascript" type="text/javascript">window.setTimeout("location=(\'' . $url . '\');", ' . $timer . '*1000);</script>';
            echo '<noscript><meta http-equiv="refresh" content="' . $timer . ';url=' . $url . '" /></noscript>';
        }else
            header('refresh: ' . $timer . ';url=' . $url);

        if ($die)
            exit;
    }

}

?>
